package com.cskaoyan.service.system;

import com.cskaoyan.bean.MallStorage;
import com.cskaoyan.bean.MallStorageExample;
import com.cskaoyan.mapper.MallStorageMapper;
import com.cskaoyan.model.bo.PageBo;
import com.cskaoyan.model.vo.fileupload.FileUploadData;
import com.cskaoyan.model.vo.PageListVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: project-market
 * @description: 对象存储管理
 * @author: Weiwei Liu
 * @create: 2021-11-30 13:49
 **/
@Service
public class StorageServiceImpl implements StorageService {

    @Autowired
    MallStorageMapper mapper;


    @Override
    public PageListVo list(PageBo pageBo, String key, String name) {

        // 设置分页信息
        PageHelper.startPage(pageBo.getPage(), pageBo.getLimit());

        MallStorageExample example = new MallStorageExample();


        // 筛选信息
        example.createCriteria().andDeletedEqualTo(false).andKeyLike(key).andNameLike(name);

        // 设置分页信息
        example.setOrderByClause(pageBo.getSort() + " " + pageBo.getOrder());


        List<MallStorage> list = mapper.selectByExample(example);

        //通过分页信息拿到分页数据
        //PageInfo的有参构造方法，放入的***查询结果***
        PageInfo info = new PageInfo(list);

        PageListVo vo = new PageListVo();

        vo.setTotal(info.getTotal());
        vo.setLimit(pageBo.getLimit());
        vo.setPage(info.getPageSize());
        vo.setPages(info.getPages());
        vo.setList(info.getList());

        return vo;

    }

    /**
     * 增加一条信息获取自增的id
     *
     * @return
     */
    @Override
    public MallStorage create(MallStorage mallStorage) {

        int i = mapper.insert(mallStorage);


        if (i == 1) {

            mallStorage.setDeleted(false);
            return mallStorage;
        }

        return null;

    }

    /**
     * 删除对象，即更新图片删除信息为TRUE
     *
     * @param mallStorage
     * @return
     */
    @Override
    public Boolean delete(MallStorage mallStorage) {

        mallStorage.setDeleted(true);
        int i = mapper.updateByPrimaryKey(mallStorage);
        return i == 1;
    }

    /**
     * 更新对象名信息
     *
     * @param mallStorage
     * @return
     */
    @Override
    public Boolean update(MallStorage mallStorage) {

        int i = mapper.updateByPrimaryKey(mallStorage);

        return i == 1;
    }
    //商品模块 图片上传 By GJC
    @Override
    public MallStorage uploadPic(FileUploadData fileUploadData) {
        MallStorage mallStorage = new MallStorage(fileUploadData.getId(),fileUploadData.getKey(),fileUploadData.getName(),fileUploadData.getType(),fileUploadData.getSize(),fileUploadData.getUrl(),fileUploadData.getAddTime(),fileUploadData.getUpdateTime(),false);
        int i = mapper.insert(mallStorage);
        return mallStorage;
    }

}
